const HttpException = require("../router/HttpException");

function resBase(data, success, code, message) {
    return {code, success, message, data}
}

function resBool(res, msg) {
    if (res) {
        return resSuccess(res, msg ? msg : '操作成功！');
    } else {
        return resFailed(200, msg ? msg : '操作失败！');
    }
}

function resSuccess(data, msg = '操作成功') {
    return resBase(data, true, 200, msg)
}

function resError(error) {
    return resBase(null, false, error.code ? error.code : 200, error.message);
}

function throwError(error, msg = '操作异常！', code = 200) {
    return new HttpException(msg + `=>${error.message}`, code);
}

function resFailed(msg, code = 200) {
    return resBase(null, false, code, msg)
}

module.exports = {
    resBase, resBool, resSuccess, resFailed, resError, throwError
}